<?php
namespace jy_weishop_en;
defined('BY_JYA') or exit('error');
/*
	购物车
*/
class cart extends \plugin{
	public function getall($selected='',$merchid='',$isnotgoodsid=false){
		$params=array('mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']);
		if($selected){
			$params['selected']=1;
		}
        $carts = pdo_getall('jy_weishop_member_cart',$params);
        $newcarts=array();
		$goodsprice=0;
		$alltotal=0;
		foreach($carts as $k=>&$cv){
			$goodsinfo=pdo_get('jy_weishop_goods',array('id'=>$cv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','title','pcate','ccate','total','unit','merchid','active_id','active_plugin'));
			$goodsinfo['price']=$cv['price'];
			$goodsinfo['optionid']=$cv['optionid'];
			$goodsinfo['storeid']=$cv['storeid'];
			$goods=$this->model('activity')->get_upgoods($goodsinfo);
			$cv['goodstotal']=intval($goods['total']);
			$cv['active']=$goods['active'];
			$cv['price']=$cv['active']['price']?:$cv['price'];
			if($merchid && $goodsinfo['merchid']!=$merchid){
				unset($carts[$k]);
				continue;
			}
			$goodsprice +=$cv['price']*$cv['total'];
			$alltotal +=$cv['total'];
			$cv['title']=$goodsinfo['title'];
			$cv['pcate']=$goodsinfo['pcate'];
			$cv['ccate']=$goodsinfo['ccate'];
			$cv['unit']=$goodsinfo['unit'];
			$cv['weight']=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$cv['goodsid']),'weight');
			if($cv['optionid']){
				$cv['options']=pdo_get('jy_weishop_goods_option',array('id'=>$cv['optionid']),array('id','stock','specs'));
				//规格
				$specids = explode("_", $cv['options']['specs']);
				$selespename = '';
				if($specids){
						foreach($specids as $k=>$sp){
							$spec_item=pdo_get('jy_weishop_goods_spec_item',array('id'=>$sp,'uniacid'=>$_SESSION['uniacid']),array('id','title'));
							$selespename .=$spec_item['title'].'，';
						}
				}
				$cv['optiontitle']= $selespename;
			}
			$key=$cv['goodsid'];
			if($cv['optionid']){
                $key .='_'.$cv['optionid'];
            }
            $newcarts[$key]=$cv;
		}
        if($isnotgoodsid){
            $newcarts=$carts;
        }
		$return['list']=$newcarts;
		$return['goodsprice']=$goodsprice;
		$return['alltotal']=$alltotal;
		return $return;
	}
	public function getordergoods($merchid=0,$store_id=0,$isbuyto=false){
		if($isbuyto){
			$cart_list=$_SESSION['cart_list'];
			$_SESSION['cart_list']='';
			if(!$cart_list){
				exi('您没有选中商品！','error');
			}
		}else{
			$cart_list=pdo_getall('jy_weishop_member_cart',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'selected'=>1),array('id','goodsid','total','optionid','merchid'));
			if(!$cart_list){
                $carturl=$_SESSION['carturl']?:'jy_weishop_en/member.cart/';
				exi('您的购物车没有选中商品！','error',$carturl);
			}
		}
		$carrier_list = pdo_getall('jy_weishop_store',array('uniacid' =>$_SESSION['uniacid'],'status'=>1,'type'=>array(1,3)),'','','paixu desc,id desc');
		//商户订单
		if($merchid){
			$merch=pdo_get('jy_merch_user',array('id'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		}
		$diyfields=array();
		foreach($cart_list as $k=>&$cart){
			$goodsfile=array('id','fang_credit2','no_de_sales','isdiyprice','diyprice','invoice','type','buylevels','buygroups','title','merchid','status','pcate','ccate','total','subtitle','image','video','price','totalcnf','weight','active_plugin','active_id','storeids','salenum','pluginname','originalprice','usermaxbuy','minbuynum','unit','isverify','diyfields','api_id','api_pluginname');
			$goods=pdo_get('jy_weishop_goods',array('id'=>$cart['goodsid']),$goodsfile);
			if(!$goods){
				exi('指定商品不存在！','error','goback');
			}
			$goods['weight']=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$cart['goodsid'],'uniacid'=>$_SESSION['uniacid']),'weight');
			if(($merchid>0 && $goods['merchid']!=$merchid) || ($merchid<0 && $goods['merchid'])){
				unset($cart_list[$k]);
				continue;			
			}
			if($_SESSION['is_tomedia']){
				$goods['image']=$goods['image']?tomedia($goods['image']):'';
			}
			$goods['diyfields']=$this->model('goods')->get_diyformfields($cart['goodsid']);
			if($goods['diyfields']){
				$diyfields=$goods['diyfields'];
			}
			//配送设置
			$goodsdispatch=$this->model('goods')->get_dispatch($cart['goodsid']);
			$goods=array_merge($goods,$goodsdispatch);
			$goods['storeid']=$store_id;
			$goods['optionid']=$cart['optionid'];
			$goods=$this->model('activity')->get_upgoods($goods);
			if($goods['active']['dian_mid']){
				$goods['merchid']=pdo_getcolumn('jy_merch_user',array('mid'=>$goods['active']['dian_mid'],'uniacid'=>$_SESSION['uniacid']),'id');
				$merchid=$goods['merchid'];
			}
			//重置下单数量
			$goods['goodstotal']=$goods['total'];
			if($cart['optionid']){
				if(!$goods['options']){
					exi('指定规格的商品不存在！','error','goback');
				}
				if(!$goods['options']['specs']){
					$optiontitle=$goods['options']['title'];
					$goods['istaocan'] = 1;
				}else{
					$goods['price']=$goods['options']['price'];
					$specs = explode("_", $goods['options']['specs']);
					$specsall = pdo_getall('jy_weishop_goods_spec_item',array('id'=>$specs,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
					if($specsall){
						$optiontitle='';
						foreach($specsall as $k=>&$sp){
							if($k==0){
								$optiontitle .=$sp['title'];
							}else{
								$optiontitle .='，'.$sp['title'];
							}
						}
					}
				}
				$goods['optiondata']= $optiontitle;
				$goods['weight']=$goods['options']['weight']?:$goods['weight'];
			}
			//来自商户的分销
			$cart['merchid']=$cart['merchid']?$cart['merchid']:$goods['merchid'];
			if($goods['type']==1 || $goods['type']==2){
				//虚拟商品
				$order['sendtype']=3;
			}elseif($goods['type']==3){
				//计时计次服务，发货类型必须是上门核销
				$order['sendtype']=2;
			}elseif($goods['type']==0 || $goods['isverify']){
				//实体商品，查找是否有门店，如果有门店则表示支持自提，否则只能是快递配送
				if(!$carrier_list && ($merchid<=0 || !$merchid)){
					$order['sendtype']=1;
				}else{
					$return['sendtypes']=array(
						'1'=>'快递配送',
						'2'=>'自提',
					);
				}
			}
			$cart=array_merge($goods,$cart);
			$cart['cartid']=$cart['id'];
			$cart['id']=$goods['id'];
		}
		$res=array(
			'sendtype'=>$order['sendtype'],
			'sendtypes'=>$return['sendtypes'],
			'diyfields'=>$diyfields,
			'cart_list'=>$cart_list,
		);
		return $res;
	}
	
}